package cn.xie.sdk.openai.code.review.sdk.domain.service.impl;

import cn.xie.sdk.openai.code.review.sdk.domain.model.Model;
import cn.xie.sdk.openai.code.review.sdk.domain.service.AbstractOpenAiCodeReviewService;
import cn.xie.sdk.openai.code.review.sdk.infrastructure.git.GitCommand;
import cn.xie.sdk.openai.code.review.sdk.infrastructure.openai.IOpenAI;
import cn.xie.sdk.openai.code.review.sdk.infrastructure.openai.dto.ChatCompletionRequestDTO;
import cn.xie.sdk.openai.code.review.sdk.infrastructure.openai.dto.ChatCompletionSyncResponseDTO;
import cn.xie.sdk.openai.code.review.sdk.infrastructure.weixin.Weixin;
import cn.xie.sdk.openai.code.review.sdk.infrastructure.weixin.dto.TemplateMessageDTO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: xiaoxie
 * create: 2025-05-31 16:08
 * @BelongsProject: openai-code-review
 * @BelongsPackage: cn.xie.sdk.openai.code.review.sdk.domain.service.impl
 * description:
 */
public class OpenAiCodeReviewService extends AbstractOpenAiCodeReviewService {

    public OpenAiCodeReviewService(GitCommand gitCommand, IOpenAI openAI, Weixin weiXin) {
        super(gitCommand, openAI, weiXin);
    }

    @Override
    protected String getDiffCode() throws IOException, InterruptedException {
        return gitCommand.diff();
    }

    @Override
    protected String codeReview(String diffCode) throws Exception {
        ChatCompletionRequestDTO chatCompletionRequest = new ChatCompletionRequestDTO();
        chatCompletionRequest.setModel(Model.GLM_4_FLASH.getCode());
        chatCompletionRequest.setMessages(new ArrayList<ChatCompletionRequestDTO.Prompt>() {
            private static final long serialVersionUID = -7988151926241837899L;

            {
                add(new ChatCompletionRequestDTO.Prompt("user", "尊敬的高级编程架构师，您拥有丰富的架构设计经验、精湛的编程技艺以及对各类场景方案的深刻洞察力。" +
                        "现在，恳请您以专业严谨且全面细致的态度，依据提供的 git diff 记录，对本次代码提交进行深入的评审。" +
                        "具体要求如下：全面分析代码的结构合理性、逻辑连贯性、功能完整性以及性能优化等方面；" +
                        "精准找出代码中可能存在的潜在缺陷、风险点以及不符合最佳实践之处；" +
                        "针对发现的问题，给出具体且具有可操作性的改进建议；" +
                        "同时，对代码的优点进行客观肯定。最终，以清晰、条理的方式呈现评审结果，" +
                        "包括总结性的评价以及分点详细阐述的优缺点和建议，以便开发人员能据此有效提升代码质量。代码如下："));
                add(new ChatCompletionRequestDTO.Prompt("user", diffCode));
            }
        });

        ChatCompletionSyncResponseDTO completions = openAI.completions(chatCompletionRequest);
        ChatCompletionSyncResponseDTO.Message message = completions.getChoices().get(0).getMessage();
        return message.getContent();
    }

    @Override
    protected String recordCodeReview(String recommend) throws Exception {
        return gitCommand.getCodeReviewLogUrl(recommend);
    }

    @Override
    protected void pushMessage(String logUrl) throws Exception {
        Map<String, Map<String, String>> data = new HashMap<>();
        TemplateMessageDTO.put(data, TemplateMessageDTO.TemplateKey.REPO_NAME, gitCommand.getProject());
        TemplateMessageDTO.put(data, TemplateMessageDTO.TemplateKey.BRANCH_NAME, gitCommand.getBranch());
        TemplateMessageDTO.put(data, TemplateMessageDTO.TemplateKey.COMMIT_AUTHOR, gitCommand.getAuthor());
        TemplateMessageDTO.put(data, TemplateMessageDTO.TemplateKey.COMMIT_MESSAGE, gitCommand.getMessage());
        weiXin.sendTemplateMessage(logUrl, data);
    }

}
